const app = getApp();
import { getInfo } from "../http/my";
export const getToken = function() {
    return new Promise((resolve, reject) => {
        const token = wx.getStorageSync('userInfo').token || ''
        resolve(token)
    })
}

export const goToShop = function() {
    const { token, unionid } = wx.getStorageSync('userInfo')
    const extraData = { token, unionid }
    wx.navigateToMiniProgram({
        appId: 'wxc0729de90389a57b',
        path: 'pages/Loading/index',
        extraData: extraData,
    })
}

/*获取当前页带参数的url*/
export const getUrl = function() {
    const pages = getCurrentPages() //获取加载的页面
    const currentPage = pages[pages.length - 1] //获取当前页面的对象
    const url = currentPage.route //当前页面url
    const tab = currentPage.getCurrentTab && currentPage.getCurrentTab()
    wx.setStorageSync('tab', JSON.stringify(tab))

    const options = currentPage.options //如果要获取url中所带的参数可以查看options

    //参数多时通过&拼接url的参数
    let urlWithArgs = url + '?'
    for (var key in options) {
        var value = options[key]
        urlWithArgs += key + '=' + value + '&'
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
    wx.setStorageSync('url', JSON.stringify(urlWithArgs))
}
// 去授权
export const getAuth = () => {
    wx.showModal({
        title: '提示',
        content: '需授权查看，请前往授权',
        cancelText: '取消',
        confirmText: '去授权',
        success(res) {
            if (res.cancel) {

            } else if (res.confirm) {
                getUrl()
                wx.navigateTo({
                    url: '/pages/login/login',
                })
            }
        }
    })
}
export const getPersonalInfo = async function() {
    const { data } = await getInfo()
    wx.setStorageSync('personalInfo', data)
    app.globalData.personalInfo = data;
}

export function handleCardNum(card) {
    let num = card.replace(/[^0-9]/ig, "")
    const str = card.replace(/[0-9]/g, "")
    num = num.replace(/(\d{4})/g, '$1 ').replace(/\s*$/, '');
    return `${str} ${num}`
}